<?php

namespace App\Admin\Metrics\Examples;

use App\Models\User;
use Dcat\Admin\Widgets\Metrics\Card;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;

class TotalUsers extends Card
{
    /**
     * 卡片底部内容.
     *
     * @var string|Renderable|\Closure
     */
    protected $footer;

    /**
     * 初始化卡片.
     */
    protected function init()
    {
        parent::init();

        $this->title('Total Users');
        $this->subTitle('最近30天'); //副标题
        $this->dropdown([
            '7' => 'Last 7 Days',
            '28' => 'Last 28 Days',
            '30' => 'Last Month',
            '365' => 'Last Year',
            'All' => '全部',
        ]);
    }

    /**
     * 处理请求.
     *
     * @param Request $request
     *
     * @return void
     */
    public function handle(Request $request)
    {
       //查询会员数
        $sum= User::all()->count();

        //7天，28天，1月，1年内
        $days7      = date("Y-m-d", strtotime("-7 days"));//当前7天内
        $daysBefore7      = date("Y-m-d", strtotime("-14 days")); //上一个7天

        $days28     = date("Y-m-d", strtotime("-28 days"));
        $daysBefore28     = date("Y-m-d", strtotime("-56 days"));

        $months     = date("Y-m-d", strtotime("-1 months"));
        $monthsBefore     = date("Y-m-d", strtotime("-2 months"));

        $lastYears  = date("Y-m-d", strtotime("-1 years"));
        $lastYearsBefore  = date("Y-m-d", strtotime("-2 years"));

        $sumDays7   = User::all()->where('created_at','>',$days7)->count();
        $sumDays7X   = User::all()->whereBetween('created_at',[$daysBefore7,$days7])->count();

        $sumDays28  = User::all()->where('created_at','>',$days28)->count();
        $sumDays28X  = User::all()->whereBetween('created_at',[$daysBefore28,$days28])->count();

        $sumMonths  = User::all()->where('created_at','>',$months)->count();
        $sumMonthsX  = User::all()->whereBetween('created_at',[$monthsBefore,$months])->count();

        $sumYears   = User::all()->where('created_at','>',$lastYears)->count();
        $sumYearsX   = User::all()->whereBetween('created_at',[$lastYearsBefore,$lastYears])->count();

        $sumAll     = User::all()->count();
//        var_dump($days28);
//        var_dump($daysBefore28);
//        var_dump($sumDays28);
//        var_dump($sumDays28X);
//        var_dump($sumDays30);
//        var_dump($sumMonths);
//        var_dump($sumYears);
//        var_dump($sumAll);

        switch ($request->get('option')) {

            case '365':
                $this->content($sumYears);
                if($sumYears > $sumYearsX){
                    $this->up($sumYears / $sumYearsX);
                }else{
                    $this->down($sumYears / $sumYearsX);
                }
                break;
            case '30':
                $this->content($sumMonths);
                if($sumMonths > $sumMonthsX){
                    $this->up($sumMonths / $sumMonthsX);
                }else{
                    $this->down($sumMonths / $sumMonthsX);
                }
                break;
            case '28':
                $this->content($sumDays28);
                if($sumDays28 > $sumDays28X){
                    $this->up($sumDays28 / $sumDays28X);
                }else{
                    $this->down($sumDays28 / $sumDays28X);
                }
                break;
            case '7':
                $this->content($sumDays7);
                if($sumDays7 > $sumDays7X){
                    $this->up($sumDays7 / $sumDays7X);
                }else{
                    $this->down($sumDays7 / $sumDays7X);
                }
                break;
            case 'All':
                $this->content($sumAll);
                $this->up(mt_rand(12, 50));
                break;
            default:
                $this->content($sum);
                $this->up(15);
        }
    }

    /**
     * @param int $percent
     *
     * @return $this
     */
    public function up($percent)
    {
        return $this->footer(
            "<i class=\"feather icon-trending-up text-success\"></i> {$percent}% Increase"
        );
    }

    /**
     * @param int $percent
     *
     * @return $this
     */
    public function down($percent)
    {
        return $this->footer(
            "<i class=\"feather icon-trending-down text-danger\"></i> {$percent}% Decrease"
        );
    }

    /**
     * 设置卡片底部内容.
     *
     * @param string|Renderable|\Closure $footer
     *
     * @return $this
     */
    public function footer($footer)
    {
        $this->footer = $footer;

        return $this;
    }

    /**
     * 渲染卡片内容.
     *
     * @return string
     */
    public function renderContent()
    {
        $content = parent::renderContent();

        return <<<HTML
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
    <h2 class="ml-1 font-lg-1">{$content}</h2>
</div>
<div class="ml-1 mt-1 font-weight-bold text-80">
    {$this->renderFooter()}
</div>
HTML;
    }

    /**
     * 渲染卡片底部内容.
     *
     * @return string
     */
    public function renderFooter()
    {
        return $this->toString($this->footer);
    }
}
